/**
 * Copyright 2009-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.myjung.dao;

import java.util.Collection;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;

import net.myjung.Myjung;
import net.myjung.model.Query;
import net.myjung.model.Order;
import net.myjung.model.QueryParameters;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.jdo.JdoObjectRetrievalFailureException;
import org.springframework.orm.jdo.support.JdoDaoSupport;

/**
 * @author Steve M. Jung
 */
public abstract class AbstractDaoJdo extends JdoDaoSupport implements IDao {
	protected final Log logger = LogFactory.getLog(getClass());

	protected Object get(Class<?> clazz, Object id, String level) throws Exception {
		try {
			Object obj = this.getJdoTemplate().getObjectById(clazz, id);
			if (level == null || level.equals(LEVEL_ALL))
				this.getPersistenceManager().retrieveAll(obj);
			if (obj != null)
				obj = this.getJdoTemplate().detachCopy(obj);
			return obj;
		} catch (JDOObjectNotFoundException e) {
			return null;
		} catch (JdoObjectRetrievalFailureException e) {
			return null;
		}
	}
	protected void set(Object obj) throws Exception {
		this.getJdoTemplate().makePersistent(obj);
	}
	protected void delete(Class<?> clazz, Object id) throws Exception {
		try {
			this.getJdoTemplate().deletePersistent(this.getJdoTemplate().getObjectById(clazz, id));
		} catch (JDOObjectNotFoundException e) {
		} catch (JdoObjectRetrievalFailureException e) {
		}
	}
	protected void deleteAll(Collection<?> col) throws Exception {
		if (Myjung.isEmpty(col))
			return;
		this.getJdoTemplate().deletePersistentAll(col);
	}
	protected void populateQuery(javax.jdo.Query query, Query conds) throws Exception {
		if (conds == null)
			return;

		// Sort Orders
		if (!Myjung.isEmpty(conds.getOrder())) {
			StringBuffer buf = new StringBuffer();
			int i = 0;
			for (Order order : conds.getOrder()) {
				if (i++ != 0)
					buf.append(", ");
				buf.append(order.getField()).append(order.isAsc() ? " asc" : " desc");
			}
			query.setOrdering(buf.toString());
		}

		// Query Ranges
		int pageNo = conds.getPageIndex();
		int pageSize = conds.getPageSize();
		if (pageSize > 0 && pageNo > -1) {
			long startNo = (long) pageNo * (long) pageSize;
			long endNo = (long) startNo + (long) pageSize;
			query.setRange(startNo, endNo);
		}
	}

	public List<?> executeSqlQuery(String sql, QueryParameters parameters, Class<?> resultClass) throws Exception {
		return null;
	}
	public <T> T delete(T obj) throws Exception {
		try {
			this.getJdoTemplate().deletePersistent(obj);
		} catch (JDOObjectNotFoundException e) {
		} catch (JdoObjectRetrievalFailureException e) {
		}
		return obj;
	}
}
